package net.chrisrichardson.ec2deployer.applications

import groovy.mock.interceptor.MockFor
import net.chrisrichardson.ec2deployer.core.AwsUtilities
import net.chrisrichardson.ec2deployer.core.EC2Server
import net.chrisrichardson.ec2deployer.core.AWSPropertiesMother
import net.chrisrichardson.ec2deployer.core.AWSProperties

public class DirectorySnapshotTest extends GroovyTestCase {

    MockFor mockEC2Server
    MockFor mockAwsUtilities

    AWSProperties awsProperties

    protected void setUp() {
        super.setUp();
        this.awsProperties = AWSPropertiesMother.makeAWSProperties()
        this.mockEC2Server = new MockFor(EC2Server)
        this.mockAwsUtilities = new MockFor(AwsUtilities)
    }


    public void testCreate() {

        mockEC2Server.demand.getAwsProperties {  awsProperties }
        mockEC2Server.demand.ssh { cmd ->
            assertEquals "cd /var/somedirectory; tar cf /var/tmp/snapshot.tar .", cmd
        }

        mockAwsUtilities.demand.s3put { String s3Path, String localPath ->
            assert s3Path.startsWith("MyAccessKey")
            assertEquals "/var/tmp/snapshot.tar", localPath
        }

        mockEC2Server.use {
            mockAwsUtilities.use {
                EC2Server host = new EC2Server()
                assertNotNull DirectorySnapshot.create(host, "/var/somedirectory")
            }
        }
    }

    public void testCreateInstall() {

        mockEC2Server.demand.getAwsProperties {  awsProperties }
        mockEC2Server.demand.ssh { cmd ->
            assertEquals "cd /var/somedirectory; tar cf /var/tmp/snapshot.tar .", cmd
        }

        def snapshotPath

        mockAwsUtilities.demand.s3put { String s3Path, String localPath ->
            assert s3Path.startsWith("MyAccessKey")
            snapshotPath = s3Path
            assertEquals "/var/tmp/snapshot.tar", localPath
        }
        mockAwsUtilities.demand.s3get { String s3Path, String localPath ->
            assertEquals snapshotPath, s3Path
            assertEquals "/var/tmp/snapshot.tar", localPath
        }

        mockEC2Server.demand.ssh { cmd ->
            assertEquals "cd /var/somedirectory; tar xf /var/tmp/snapshot.tar", cmd
        }

        mockEC2Server.use {
            mockAwsUtilities.use {
                EC2Server host = new EC2Server()
                def ds = DirectorySnapshot.create(host, "/var/somedirectory")
                ds.install host 
            }
        }
    }


}